package com.jiang.demo2.control;

import com.jiang.demo2.bean.Result;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

public class BaseControl {


    /**
     * 缺少必要参数
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result handle(MissingServletRequestParameterException e) {
        String parameterName = e.getParameterName();
        return Result.fail(1, "缺少必要的参数" + parameterName );

    }


    /**
     * 参数类型不匹配
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public Result handle(MethodArgumentTypeMismatchException e) {
        return Result.fail(2, "参数类型不匹配" + e.getName()+"="+ e.getValue());
    }

}
